app.controller("userCtrl", function ($scope, $http, $window, userService, idService) {

    $scope.principle = {};
    $scope.isManager = false;
    $scope.isTester = false;
    $scope.isDeveloper = false;
    $scope.isRequire = false;

    idService.principle().then(function(value) {$scope.principle = value.data;});
    idService.isManager().then(function(value) {$scope.isManager = value.data;});
    idService.isTester().then(function(value) {$scope.isTester = value.data;});
    idService.isDeveloper().then(function(value) {$scope.isDeveloper = value.data;});
    idService.isRequire().then(function(value) {$scope.isRequire = value.data;});

    $scope.showList = true;
    $scope.showGroup = false;

    $scope.toList = function() {
        $scope.showList = true;
        $scope.showGroup = false;
    };
    $scope.toGroup = function() {
        $scope.showList = false;
        $scope.showGroup = true;
    };

    $scope.userList = [];
    $scope.groupList = [];
    $scope.groupUserList = [];
    $scope.user = {
        "password": "123456"
    };

    $scope.getAllUser = function () {
        $http.get(
            "/user/page"
        ).then(function (response) {
            $scope.userList = response.data.data;
        })
    };

    $scope.getAllGroup = function () {
        $http.get(
            "/group/list"
        ).then(function (response) {
            $scope.groupList = response.data.data;
        })
    };

    $scope.getGroupUser = function (group) {
        $scope.currentGroup = group.name + "(" + group.id + ")";
        $http.get(
            "/user/list?groupId=" + group.id
        ).then(function (response) {
            $scope.groupUserList = response.data.data;
        })
    };

    $scope.addUser = function () {
        checkGroup();
        $http.post(
            "/user/add",
            $scope.user
        ).then(function (response) {
            if (response.data.code === 1) {
                alert("操作成功！");
                $window.location.reload();
            }
        })
    };

    function checkGroup() {
        $scope.user.groupsStr = "";
        angular.forEach($scope.groupList,function(data, index, array){
            if (data.checked) {
                $scope.user.groupsStr += data.id + ",";
            }
        });
    }

});